import { parse } from 'querystring';
import ExportJsonExcel from 'js-export-excel';

/* eslint no-useless-escape:0 import/prefer-default-export:0 */

const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
export const isUrl = path => reg.test(path);
export const isAntDesignPro = () => {
  if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
    return true;
  }

  return window.location.hostname === 'preview.pro.ant.design';
}; // 给官方演示站点用，用于关闭真实开发环境不需要使用的特性

export const isAntDesignProOrDev = () => {
  const { NODE_ENV } = process.env;

  if (NODE_ENV === 'development') {
    return true;
  }

  return isAntDesignPro();
};
export const getPageQuery = () => parse(window.location.href.split('?')[1]);

// 表单
export const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 8 },
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 16 },
  },
};

// 格式化菜单
export const formatMenu = param => {
  const menus = []
  param && param.map(e =>
    e.childMenus ?
    menus.push({
      path: e.menuUrl,
      name: e.name,
      icon: e.icon,
      id: e.id,
      button: e.elements,
      children: formatMenu(e.childMenus)
    }) :
    menus.push({
      path: e.menuUrl,
      name: e.name,
      icon: e.icon,
      id: e.id,
      button: e.elements,
    })
  )
  return menus
}

// 是否登录
export const isLogin = (localStorage.getItem('user') && JSON.parse(localStorage.getItem('user')).id) || (sessionStorage.getItem('user') && JSON.parse(sessionStorage.getItem('user')).id);

// 获取token
export const getToken = (localStorage.getItem('user') || sessionStorage.getItem('user')) ? JSON.parse(localStorage.getItem('user')).token || JSON.parse(sessionStorage.getItem('user')).token : null

// 获取suppliedId
export const supplierId = (localStorage.getItem('user') && JSON.parse(localStorage.getItem('user')).supplierId) || (sessionStorage.getItem('user') && JSON.parse(sessionStorage.getItem('user')).supplierId);

export const exportsExcel = (params) => {
  let option = {};
  // 获取表头
  const filterCol = []
  const colArr = []
  params.columns.filter(c => c.dataIndex != 'sort').map(e => {
    colArr.push(e.title)
    filterCol.push(e.dataIndex)
  })

  option.fileName = params.title || 'sheet'
  option.datas=[
    {
      sheetData: params.data,
      sheetName: params.title || 'sheet',
      sheetHeader: colArr,
      sheetFilter: filterCol,
      columnWidths: params.widths || [5, 10, 5, 10, 5, 10, 5, 5, 5, 15]
    }
  ];

  const toExcel = new ExportJsonExcel(option);
  toExcel.saveExcel();
}
